package com.rjj.wlqq.ryAnnotationExcel;

import com.rjj.wlqq.ryAnnotationExcel.entity.Dept;
import com.rjj.wlqq.ryAnnotationExcel.entity.User;
import com.rjj.wlqq.tool.excel.ry_excel.ExcelUtil;
import com.rjj.wlqq.tool.excel.ry_excel.StringUtils;
import com.rjj.wlqq.tool.file.FileUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author r
 * @date 2020/12/12 12:02
 */
@Controller
public class RyExcelController {

    /**
     * ry中页面
     */
    @RequestMapping("/ryIndex")
    public String index() {
        return "/ryExcel/ryIndex";
    }

    @RequestMapping("/ryDownloadExcel")
    public void download(HttpServletResponse response) throws Exception {
        InputStream inputStream = ResourceUtils.getURL("classpath:templates/ryExcel/ryExportExcel.xls").openStream();
        response.setContentType("application/force-download;charset=UTF-8");
        final String name = "aa.xls";
        String contentDispositionValue = "attachment; filename=" +
                name + ";" +
                "filename*=" + "utf-8''" +
                name;
        response.setHeader("Content-disposition", contentDispositionValue);
        IOUtils.copy(inputStream, response.getOutputStream());
    }

    /**
     * 导出
     * @param response 响应
     * @throws Exception 异常
     */
    @RequestMapping(value = "/ryExportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        ExcelUtil<User> excelUtil = new ExcelUtil<>(User.class);
        final ArrayList<User> users = new ArrayList<>();
        users.add(initUser());

        String fileName = "下载名称.xls";
        if (!FileUtils.checkAllowDownload(fileName)) {
            throw new Exception(StringUtils.format("文件名称({})非法，不允许下载。 ", fileName));
        }
        String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FileUtils.setAttachmentResponseHeader(response, realFileName);

        try {
            excelUtil.exportExcel(response, users, "rjjSheet");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入的时候如果注解中有标示 type = Excel.Type.EXPORT 那导入就没有数据
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/ryImportExcel")
    public List<User> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelUtil<User> excelUtil = new ExcelUtil<>(User.class);
        final List<User> users = excelUtil.importExcel(file.getInputStream());
        return users;
    }


    /**
     * 初始化部门数据
     */
    public Dept initDept() {
        final Dept dept = new Dept();
        dept.setDeptName("rjj");
        dept.setLeader("leader-rjj");
        return dept;
    }

    /**
     * 初始化用户数据
     */
    public User initUser() {
        final User user = new User();
        user.setId(1);
        user.setName("rjj-rjj");
        user.setAge(10);
        user.setSex(1);
        user.setDept(initDept());
        user.setLoginDate(new Date());
        return user;
    }

}
